<template>
  <div class="change-password">
    <h2>修改密码</h2>

    <form @submit.prevent="changePassword">
      <div class="form-group">
        <label for="oldPassword">原密码：</label>
        <input type="password" id="oldPassword" v-model="oldPassword" required />
      </div>

      <div class="form-group">
        <label for="newPassword">新密码：</label>
        <input type="password" id="newPassword" v-model="newPassword" required />
        <p v-if="showLengthError" class="error-message">密码长度至少为6位</p>
      </div>

      <div class="form-group">
        <label for="confirmPassword">确认新密码：</label>
        <input type="password" id="confirmPassword" v-model="confirmPassword" required />
        <p v-if="showMatchError" class="error-message">两次输入的密码不一致</p>
      </div>

      <div class="form-actions">
        <button type="submit" :disabled="!isFormValid">确认修改</button>
        <router-link to="/customer/home" class="btn-back">返回</router-link>
      </div>
    </form>

    <p v-if="message" class="message" :class="{ 'error': isError }">{{ message }}</p>
  </div>
</template>

<script>
export default {
  name: 'ChangePasswordCustomer',
  data() {
    return {
      oldPassword: '',
      newPassword: '',
      confirmPassword: '',
      message: '',
      isError: false,
      showLengthError: false,
      showMatchError: false
    }
  },
  computed: {
    isFormValid() {
      return (
          this.newPassword.length >= 6 &&
          this.newPassword === this.confirmPassword &&
          this.newPassword !== this.oldPassword
      )
    }
  },
  watch: {
    newPassword() {
      this.showLengthError = this.newPassword.length > 0 && this.newPassword.length < 6
      this.showMatchError = this.newPassword !== this.confirmPassword
      this.checkPasswordSame()
    },
    confirmPassword() {
      this.showMatchError = this.newPassword !== this.confirmPassword
    }
  },
  methods: {
    checkPasswordSame() {
      if (this.newPassword === this.oldPassword && this.newPassword.length > 0) {
        this.message = '新密码不能与原密码相同'
        this.isError = true
      } else if (this.message === '新密码不能与原密码相同') {
        this.message = ''
        this.isError = false
      }
    },
    async changePassword() {
      // 表单验证
      if (this.newPassword.length < 6) {
        this.message = '新密码长度至少为6位'
        this.isError = true
        return
      }

      if (this.newPassword !== this.confirmPassword) {
        this.message = '两次输入的密码不一致'
        this.isError = true
        return
      }

      if (this.newPassword === this.oldPassword) {
        this.message = '新密码不能与原密码相同'
        this.isError = true
        return
      }

      const contactName = this.$store.state.user?.contactName
      if (!contactName) {
        this.message = '未找到当前用户'
        this.isError = true
        return
      }

      try {
        const response = await this.$axios.put('/auth/customer/change-password', null, {
          params: {
            name: contactName,
            oldPassword: this.oldPassword,
            newPassword: this.newPassword
          }
        })

        this.message = response.data
        this.isError = false

        // 清空表单
        this.oldPassword = ''
        this.newPassword = ''
        this.confirmPassword = ''
      } catch (error) {
        this.message = error.response?.data || '修改密码失败，请重试'
        this.isError = true
      }
    }
  }
}
</script>

<style scoped>
.change-password {
  padding: 20px;
}

.form-group {
  margin-bottom: 15px;
}

label {
  display: block;
  font-weight: bold;
}

input[type="password"] {
  width: 100%;
  padding: 8px;
  margin-top: 5px;
  border-radius: 4px;
  border: 1px solid #ccc;
}

.error-message {
  color: red;
  font-size: 14px;
  margin-top: 5px;
}

.form-actions {
  margin-top: 20px;
}

button {
  padding: 10px 20px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: bold;
}

button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

button:hover:not(:disabled) {
  background-color: #369c6b;
}

.btn-back {
  margin-left: 10px;
  text-decoration: none;
  color: #333;
  background-color: #eee;
  padding: 10px 20px;
  border-radius: 4px;
}

.btn-back:hover {
  background-color: #ddd;
}

.message {
  margin-top: 20px;
  font-weight: bold;
}

.message.error {
  color: red;
}

.message:not(.error) {
  color: green;
}
</style>